﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Linq;
using RookieGrow.Infra.NHib;
using System.Configuration;
using RookieGrow.Domain.Model.NhSpStore;
using RookieGrow.Domain.Model.NhSpStore.Map;
using RookieGrow.Infra.NHib;

namespace RookieGrow.Data.SportsStore.NHib
{
    public class NhProductRepository : INhProductRepository
    {
        private ISessionFactory sessionFactory;
        
        public NhProductRepository()
        {
            NHibernateDb<ProductMap> db = new NHibernateDb<ProductMap>(ConfigurationManager.ConnectionStrings["SportsDb"].ConnectionString, DbType.MYSQL);
            sessionFactory = db.SessionFactory;
        }

        public List<Product> GetProducts()
        {
            using (var session = sessionFactory.OpenSession())
            {
                return session.Query<Product>()
                    .OrderBy(c => c.Id)
                    .ToList();
            }
        }
    }
}
